#include "main.h"

#define UDP_BUF_LEN     (1472)
static char buf[UDP_BUF_LEN];

using namespace std;


int udp_send_test()
{
    struct sockaddr_in servaddr;
    int sockfd;
    UINT64 ull_send_len = 0;

    memset(buf, 0xAB, sizeof(buf));

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    inet_pton(AF_INET, SERVER_IP, &servaddr.sin_addr.s_addr);
    servaddr.sin_port = htons(SERVER_PORT);

    MakeSocketNonBlocking(sockfd);

    struct timeval st_init_time;
    INT64 ll_sys_ms_time;
    sys_time_getms_r(st_init_time, st_init_time, &ll_sys_ms_time);

    UINT32 ui_seq = 0;

    INT32 i_send_len = 0;
    for (ui_seq = 0;ui_seq < 0xffffffff; ) {

        struct timeval st_now_time;
        sys_time_getms_r(st_init_time, st_now_time, &ll_sys_ms_time);
        if (ll_sys_ms_time > 1000) {
            st_init_time = st_now_time;
            i_send_len = 0;
        } else {
            if (i_send_len > (10*1024*1024)) {
                ms_msleep(1);
                continue;
            }
        }

        VOD_DATA_HEAD_S *pst_recv = (VOD_DATA_HEAD_S *)buf;
        pst_recv->ui_Magic = REPLAY_HEAD_MAGIC;
        pst_recv->ui_total_len = UDP_BUF_LEN;
        pst_recv->ui_seq = ui_seq++;
        int i_ret = sendto(sockfd, buf, sizeof(buf), 0, (struct sockaddr*)&servaddr, sizeof(servaddr));
        if (i_ret > 0)  {
            ull_send_len += 1500;
            i_send_len += 1500;
        } else {
            ms_msleep(1);
        }
    }

    close(sockfd);

    return 0;
}

